export const ctime = (nowtime) => {
  let oDate = new Date(nowtime),
    oYear = oDate.getFullYear(),
    oMonth = oDate.getMonth() + 1,
    oDay = oDate.getDate(),
    oHour = oDate.getHours(),
    oMin = oDate.getMinutes(),
    oSen = oDate.getSeconds(),
    oTime = oYear + '-' + oMonth + '-' + oDay + ' ' + oHour + ':' + oMin + ':' + oSen; //最后拼接时间
  return oTime;
}

export const format = (timeStamp, fmt) => {
  let data = new Date(timeStamp)
  let o = {
    "M+": data.getMonth() + 1, //月份
    "d+": data.getDate(), //日
    "h+": data.getHours(), //小时
    "m+": data.getMinutes(), //分
    "s+": data.getSeconds(), //秒
    "q+": Math.floor((data.getMonth() + 3) / 3), //季度
    "S": data.getMilliseconds() //毫秒
  };
  if (/(y+)/.test(fmt)) {
    fmt = fmt.replace(RegExp.$1, (data.getFullYear() + "").substr(4 - RegExp.$1.length));
  }
  for (let k in o) {
    if (new RegExp("(" + k + ")").test(fmt)) {
      fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    }
  }
  return fmt;
}
//特殊字符验证
export const formatInput = (val) => {
  var reg = new RegExp("[` ~!@#$^&*()=|{}':;',\\[\\].<>/?~！@#￥……&*（）——|{}【】‘；：”“'。，、？ % + -]");
  var rs = "";
  for (var i = 0, l = val.length; i < val.length; i++) {
    rs = rs + val.substr(i, 1).replace(reg, '');
  }
  return rs;
}
// 去除空格
//权限列表遍历
export const has = (functionId, vue) => {
  let authIdList = vue.$store.getters.getAuthList;
  if (authIdList.indexOf(functionId) > -1) {
    return true
  }
}

// 导出excel
export const exportToExcel = (header, filter, lists, title) => {
  require.ensure([], () => {
    const {
      export_json_to_excel
    } = require('@/vendor/Export2Excel')
    const tHeader = header
    const filterVal = filter
    const list = lists
    const data = formatJson(filterVal, list)
    export_json_to_excel(tHeader, data, title)
    //delete vue.list[vue.list.length]
  })
}
// 解析json
function formatJson(filterVal, jsonData) {
  return jsonData.map(v => filterVal.map(j => v[j]))
}
// excel转json

export const importToJson = (data) => {
  var wb; //读取完成的数据
  var rABS = false; //是否将文件读取为二进制字符串
  wb = XLSX.read(data, {
    type: 'binary'
  });

  return XLSX.utils.sheet_to_json(wb.Sheets[wb.SheetNames[0]])

}

// 通过年月判断改月的总天数
export const getDays = (month, year) => {
  month = parseInt(month, 10);
  var temp = new Date(year, month, 0);
  return temp.getDate();
}

// 通过年月输出日期（月天数以及星期）列表
export const getMonthDay = (date) => {
  let day = new Date().getDate();
  let dateArray = date.split('-')
  let month = parseInt(dateArray[1]);
  let year = parseInt(dateArray[0]);
  let days = getDays(month, year); // 获取总天数
  let callBackDataList = [];
  for (let i = 1; i <= days; i++) {
    //callBackDataList.push(`${month}/${i}`);
    let xq = getXq(`${date}-${i}`);
    if (i == day) {
      callBackDataList.push({
        rq: {
          rq: `${i}`,
          isToday: true
        },
        xq: {
          xq: xq,
          isToday: true
        }
      });
    } else if (xq == "六" || xq == "天") {
      callBackDataList.push({
        rq: {
          rq: `${i}`,
          isWeekend: true
        },
        xq: {
          xq: xq,
          isWeekend: true
        }
      });
    } else {
      callBackDataList.push({
        rq: {
          rq: `${i}`
        },
        xq: {
          xq: xq
        }
      });
    }


  }
  return callBackDataList
}

// 通过日期判断星期
export const getXq = (date) => {
  return '天一二三四五六'.charAt(new Date(date).getDay())
}

//消息
export const renderMessageList = (vue) => {
  let messageData = vue.$store.getters.getMessgeList;
  console.log(messageData)
  // let {
  //   checkinCount,
  //   miniCount,
  //   orderCount
  // } = messageData;
  // window.bus.$emit("pageInformation", messageData);
  // window.bus.$emit("messageCount", messageData.count);
  // vue.count = messageData.orderCount;
  // vue.checkinList = messageData.checkinList;
  // vue.miniList = messageData.miniList;
  // vue.orderList = messageData.orderList;
  // var checkItem = "";
  // var miniItem = "";
  // var orderItem = "";
  // if (!checkinCount || !miniCount || !orderCount) {
  //   vue.player.play();
  // }
  // vue.checkinList.forEach(item => {
  //   checkItem += `
  //           <a class="title" href="#/index/checkIn-detail?id=${
  //     item.omcId
  //     }&state=${item.id}">
  //             <label style="margin-right:10px;">${
  //     item.createTime
  //     }</label><label>${item.title || null}</label>
  //           </a>
  //         `;
  // });
  // vue.miniList.forEach(item => {
  //   miniItem += `
  //           <a class="title" href="#/index/list_goods_detail?id=${
  //     item.omcId
  //     }&state=${item.id}">
  //             <label style="margin-right:10px;">${
  //     item.createTime
  //     }</label><label>${item.title || null}</label>
  //           </a>
  //         `;
  // });
  // vue.orderList.forEach(item => {
  //   orderItem += `
  //           <a class="title" href="#/index/order_detail?id=${
  //     item.omcId
  //     }&state=${item.id}">
  //             <label style="margin-right:10px;">${
  //     item.createTime
  //     }</label><label>${item.title || null}</label>
  //           </a>
  //         `;
  // });

  // if (vue.checkinList.length > 0) {
  //   window.roomClosed = vue.roomClose = vue.$notify({
  //     duration: 15000,
  //     position: "bottom-right",
  //     dangerouslyUseHTMLString: true,
  //     offset: 20,
  //     onClick: () => {
  //       return false;
  //     },
  //     message: `
  //               <div class="message-window">
  //                 <div class="window-left order-bg">
  //                   <div class="window-left-top">
  //                     <i class="fa iconfont icon-37"></i>
  //                     <span>${messageData.checkinCount}</span>
  //                      <p class="window-left-bt">退房通知</p>
  //                   </div>                        

  //                 </div>
  //                 <div class="window-right">
  //                   <div class="content">                         
  //                     ${checkItem}
  //                     </div>
  //                   </div>
  //                 </div>
  //               </div>`
  //   });
  // }
  // if (vue.miniList.length > 0) {
  //   window.roomClosed = vue.roomClose = vue.$notify({
  //     duration: 15000,
  //     position: "bottom-right",
  //     dangerouslyUseHTMLString: true,
  //     offset: 140,
  //     onClick: () => {
  //       // vue.$router.push("/index/order_form");
  //       // vue.roomClose.close();
  //     },
  //     message: `
  //               <div class="message-window" @click="jumpOrder">
  //                 <div class="window-left order-bg">
  //                   <div class="window-left-top">
  //                     <i class="fa iconfont icon-xiaofeizhe"></i>
  //                     <span>${messageData.miniCount}</span>
  //                      <p class="window-left-bt">mini吧消费</p>
  //                   </div>                        

  //                 </div>
  //                 <div class="window-right">
  //                   <div class="content">                         
  //                     ${miniItem}
  //                   </div>
  //                 </div>
  //               </div>`
  //   });
  // }
  // if (vue.orderList.length > 0) {
  //   window.roomClosed = vue.roomClose = vue.$notify({
  //     duration: 15000,
  //     position: "bottom-right",
  //     dangerouslyUseHTMLString: true,
  //     offset: 260,
  //     onClick: () => {
  //       // vue.$router.push("/index/order_form");
  //       // vue.roomClose.close();
  //     },
  //     message: `
  //               <div class="message-window" @click="jumpOrder">
  //                 <div class="window-left order-bg">
  //                   <div class="window-left-top">
  //                     <i class="fa iconfont icon-dingdan"></i>
  //                     <span>${messageData.orderCount}</span>
  //                      <p class="window-left-bt">订单通知</p>
  //                   </div>                        

  //                 </div>
  //                 <div class="window-right">
  //                   <div class="content">                         
  //                     ${orderItem}
  //                   </div>
  //                 </div>
  //               </div>`
  //   });
  // }
}
//下载网路图片
//下载图片地址和图片名
export const downloadIamge = (imgsrc, name) => {
  console.log(imgsrc)
  var image = new Image();
  // 解决跨域 Canvas 污染问题
  image.setAttribute("crossOrigin", "anonymous");
  image.onload = function () {
    var canvas = document.createElement("canvas");
    canvas.width = image.width;
    canvas.height = image.height;
    var context = canvas.getContext("2d");
    context.drawImage(image, 0, 0, image.width, image.height);
    var url = canvas.toDataURL("image/png"); //得到图片的base64编码数据

    var a = document.createElement("a"); // 生成一个a元素
    var event = new MouseEvent("click"); // 创建一个单击事件
    a.download = name || "photo"; // 设置图片名称
    a.href = url; // 将生成的URL设置为a.href属性
    a.dispatchEvent(event); // 触发a的单击事件
  };
  image.src = imgsrc;
}
